package cn.itcast.web.action;

import cn.itcast.domain.system.Role;
import cn.itcast.service.RoleService;
import cn.itcast.web.action.common.BaseAction;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Actions
public class RoleAction extends BaseAction<Role> {
    @Autowired
    private RoleService roleService;

    //    url : '../../role_list.action',
    @Action(value = "role_list", results = {@Result(name = "success", type = "json")})
    public String findAll() {
        List<Role> roles = roleService.findAll();
        ServletActionContext.getContext().getValueStack().push(roles);
        return SUCCESS;
    }
    // url ../../role_save.action
    //以属相驱动方式接受permissionIds和menuIds
    private String[] permissionIds;
    private String menuIds;//id之间以逗号分割

    public void setPermissionIds(String[] permissionIds) {
        this.permissionIds = permissionIds;
    }

    public void setMenuIds(String menuIds) {
        this.menuIds = menuIds;
    }

    @Action(value = "role_save", results = {
            @Result(name = "success", location = "pages/system/role.html", type = "redirect")})
    public String save(){
       roleService.save(model,permissionIds,menuIds);
        return SUCCESS;
    }
}
